obj = {
	add:function () {
		var awin = $('#addWin');
		awin.panel({title: "新增模式"}).dialog("open");
	},
	addSon:function () {
		var node = $('#shiftConfig_data').treegrid('getSelected');
		if(node!=null){
			if(node.fatherId==null){
				var awin = $('#addSonWin');
				$("#fatherId").val(node.id);
				awin.panel({title: "新增配置"}).dialog("open");
			}else{
				$.messager.alert("提示信息","请选中一个模式!");
			}
		}else{
			$.messager.alert("提示信息","请选择一条记录!");
		}
	},
	edit:function(){
		  var node = $('#shiftConfig_data').treegrid('getSelected');
		  if(node!=null){
			  if(node.fatherId==null){
				  if(node.isEffective=="Y"){
					  $.messager.alert("提示信息","正在使用的设置不能修改!");
				  }else{
					  var ewin = $('#editWin');
					  $.post(basePath+"/shiftConfig/load.json",{'id':node.id},function(res){
						  ewin.form('load', {
							  isEffective : res.data.isEffective,
							  shiftType : res.data.shiftType,
							  start : res.data.start,
							  end:res.data.end,
							  remark:res.data.remark,
							  sequence:res.data.sequence,
							  lineId:res.data.lineId,
							  id:res.data.id
						  }).dialog('open');
					  });
				  }
			  }else{
				  var node1=$('#shiftConfig_data').treegrid('find',node.fatherId);
				  if(node1.isEffective=="Y"){
					  $.messager.alert("提示信息","正在使用的设置不能修改!");
				  }else{
					  var eSonwin = $('#editSonWin');
					  $.post(basePath+"/shiftConfig/load.json",{'id':node.id},function(res){
						  eSonwin.form('load', {
							  shiftName : res.data.shiftName,
							  start : res.data.start,
							  end:res.data.end,
							  remark:res.data.remark,
							  sequence:res.data.sequence,
							  fatherId:res.data.fatherId,
							  id:res.data.id
						  }).dialog('open');
					  });
				  }
			  }
		  }else{
			  $.messager.alert("提示信息","请选择一条记录!");
		  }
	},
	deletes:function(){
		var node = $('#shiftConfig_data').treegrid('getSelected');
		if(node!=null){
			$.ajax({
				url: basePath+"/shiftConfig/delete.json",
				type:'post',
				data:{"id":node.id},
				success:function(result,response,status){
					if(result.success){
						$.messager.alert("提示信息",result.msg);
						$('#shiftConfig_data').treegrid('reload');
					}else{
						$.messager.alert("提示信息",result.msg);
					}
				}
			});
		}else{
			$.messager.alert("提示信息","请选择一条记录!");
		}
	},
	effective:function(){
		var node = $('#shiftConfig_data').treegrid('getSelected');
		if(node!=null){
			if(node.fatherId==null){
				$.ajax({
					url: basePath+"/shiftConfig/setEffective.json",
					type:'post',
					data:{"id":node.id,"lineId":node.lineId},
					success:function(result,response,status){
						if(result.success){
							$.messager.alert("提示信息",result.msg);
							$('#shiftConfig_data').treegrid('reload');
						}else{
							$.messager.alert("提示信息",result.msg);
						}
					}
				});
			}else{
				$.messager.alert("提示信息","请选中一个班制!");
			}
		}else{
			$.messager.alert("提示信息","请选择一条记录!");
		}
	},
	noEffective:function(){
		var node = $('#shiftConfig_data').treegrid('getSelected');
		if(node!=null){
			if(node.fatherId==null){
				$.ajax({
					url: basePath+"/shiftConfig/setNOEffective.json",
					type:'post',
					data:{"id":node.id},
					success:function(result,response,status){
						if(result.success){
							$.messager.alert("提示信息",result.msg);
							$('#shiftConfig_data').treegrid('reload');
						}else{
							$.messager.alert("提示信息",result.msg);
						}
					}
				});
			}else{
				$.messager.alert("提示信息","请选中一个班制!");
			}
		}else{
			$.messager.alert("提示信息","请选择一条记录!");
		}
	}
	
};

	
$(function(){
	var lineId=0;
	$("#linetree").tree({
		url: basePath+"/lineConfig/generateTree.json",
		method: 'get',
		onClick: function(node) {
			if (node.url) {
				lineId=node.id;
				$('#shiftConfig_data').treegrid({
	    			url: basePath+"/shiftConfig/findByLineTreePage.json",
	    			queryParams:{lineId:lineId}
	    		});
			}
		}
	});
	$('#shiftConfig_data').treegrid({
//		url: basePath+"/shiftConfig/findByTreePage.json",
	    method: 'get',
	    striped : true,
		nowrap : true,
		rownumbers : true,
		fitColumns : true,
		singleSelect:true,
		border:false,
		idField: 'id',
	    treeField: 'shiftType',
		toolbar:'#tb_sb',
		scrollbarSize:1,
//		title:'设备',
		columns:[[
	          {field:'shiftType',align:'center','title':'班制',width:200},
	          {field:'shiftName',align:'center','title':'班次',width:200},
	          {field:'effectiveStartTime',align:'center','title':'开始时间',width:150,formatter: function(value){if(value){return moment(value).format("YYYY-MM-DD");}else{return null;}}},
	          {field:'effectiveEndTime',align:'center','title':'结束时间',width:150,formatter: function(value){if(value){return moment(value).format("YYYY-MM-DD");}else{return null;}}},
	          {field:'startTime',align:'center','title':'开始时刻',width:150,formatter: function(value){if(value){return moment(value).format("HH:mm");}else{return null;}}},
	          {field:'endTime',align:'center','title':'结束时刻',width:150,formatter: function(value){if(value){return moment(value).format("HH:mm");}else{return null;}}},
	          {field:'sequence',align:'center','title':'顺序',width:200},
	          {field:'remark',align:'center','title':'备注',width:200},
	          {field:'isEffective',align:'center','title':'是否有效',width:100}
        ]],
	    pagination : true,
  		pageList : [5,10],
  		pageNumber : 1,
		pageSize : 10,
  		pagePosition : 'bottom'
	});
	
	$('#dts').timespinner({    
	    required: true,    
	    showSeconds: false   
	});
	$('#dte').timespinner({    
	    required: true,    
	    showSeconds: false   
	});
	//新增配置
	$('#addSonWin').dialog({
		width:400,
		height:300,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#addSonform').form('validate')){
					$.post(basePath+"/shiftConfig/savebyShift.json",$("#addSonform").serialize(),function(data){
						if(data!=null){
							$.messager.alert({showSpeed:100,title:'消息提示',msg:data.msg,timeout:3000});
							$('#addSonWin').dialog('close');
							$('#shiftConfig_data').treegrid('reload');
						}else{
							$.messager.alert({showSpeed:100,title:'消息提示',msg:'操作失败',timeout:3000});
						}
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#addSonWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#addSonform').form('reset');
		}
	});
	$('#dts_e').timespinner({    
	    required: true,    
	    showSeconds: false   
	});
	$('#dte_e').timespinner({    
	    required: true,    
	    showSeconds: false   
	});
	//编辑配置
	$('#editSonWin').dialog({
		title:"编辑配置",
		width:400,
		height:300,
		iconCls:'icon-edit',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#editSonform').form('validate')){
					$.ajax({
						url: basePath+"/shiftConfig/updatebyShift.json",
						type:'post',
						data:$('#editSonform').serialize(),
						success:function(result,response,status){
							if(result!=null){
								$.messager.alert("提示信息",result.msg);
								$('#editSonWin').dialog('close');
								$('#shiftConfig_data').treegrid('reload');
							}else{
								$.messager.alert("提示信息","操作失败");
							}
						}
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#editSonWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#editSonform').form('reset');
		}
	});
	
	//新增模式
	$('#addWin').dialog({
		width:400,
		height:300,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#addform').form('validate')){
					$("#lineId").val(lineId);
					$.post(basePath+"/shiftConfig/savebyShifts.json",$("#addform").serialize(),function(data){
						if(data!=null){
							$.messager.alert({showSpeed:100,title:'消息提示',msg:data.msg,timeout:3000});
							$('#addWin').dialog('close');
							$('#shiftConfig_data').treegrid('reload');
						}else{
							$.messager.alert({showSpeed:100,title:'消息提示',msg:'操作失败',timeout:3000});
						}
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#addWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#addform').form('reset');
		}
	});
	//编辑模式
	$('#editWin').dialog({
		title:"编辑模式",
		width:400,
		height:300,
		iconCls:'icon-edit',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'提交',
			iconCls:'icon-add-new',
			handler:function(){
				if($('#editform').form('validate')){
					$.ajax({
						url: basePath+"/shiftConfig/updatebyShifts.json",
						type:'post',
						data:$('#editform').serialize(),
						success:function(result,response,status){
							if(result!=null){
								$.messager.alert("提示信息",result.msg);
								$('#editWin').dialog('close');
								$('#shiftConfig_data').treegrid('reload');
							}else{
								$.messager.alert("提示信息","操作失败");
							}
						}
					});
				}
			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#editWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#editform').form('reset');
		}
	});
});
